package com.ramos.kuaixiao.shops.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.entity.OrderDetail;
import com.ramos.kuaixiao.shops.entity.MallOrder;
import com.ramos.kuaixiao.shops.ui.MallOrderDetailActivity;
import com.ramos.kuaixiao.utils.StringUtil;
import com.ramos.kuaixiao.utils.Utils;

import java.util.List;

public class MallOrderAdapter extends RecyclerView.Adapter {

    private List<MallOrder> tasklist;
    private Activity context;
    private List<String> detaillist;
    private List<OrderDetail> orderllist;

    public MallOrderAdapter(Activity context, List<MallOrder> hotcourse) {
        this.context = context;
        this.tasklist = hotcourse;
    }

    public void refresh(List<MallOrder> data) {
        tasklist = data;
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return tasklist.size();
    }


    @SuppressLint("InflateParams")
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.listitem_task, null);
        ViewHodler vh = new ViewHodler(view);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHodler viewHolder = (ViewHodler) holder;
        viewHolder.tvDanju.setText("￥" + tasklist.get(position).getOrderTotalPrice());
        Log.d("OrderAdapter", "getOrderTotalPrice:"+tasklist.get(position).getOrderTotalPrice());
        viewHolder.tvKehu.setText(tasklist.get(position).getEmpName());
        Drawable icon = context.getResources().getDrawable(R.drawable.arp);
        //setBounds(left,top,right,bottom)里的参数从左到右分别是
        //drawable的左边到textview左边缘+padding的距离，drawable的上边离textview上边缘+padding的距离
        //drawable的右边边离textview左边缘+padding的距离，drawable的下边离textview上边缘+padding的距离
        //所以right-left = drawable的宽，top - bottom = drawable的高
        icon.setBounds(20, 0, 70, 50);
        viewHolder.tvKehu.setCompoundDrawables(icon, null, null, null);
        viewHolder.tvShijian.setText("" + tasklist.get(position).getOrderDate());
        viewHolder.tvMoney.setText("" + statusToStr(tasklist.get(position).getOrderStatus()));
        if(!StringUtil.isEmpty(tasklist.get(position).getDmsOrderId())){
            viewHolder.danhao.setText("订单号:" + tasklist.get(position).getDmsOrderId());
        } else {
            viewHolder.danhao.setText("订单号:" + tasklist.get(position).getOrderId());
        }
        if (tasklist.get(position).getDetailList() == null || tasklist.get(position).getDetailList().size() == 0) {
            viewHolder.num.setText("共0件商品");
        } else {
            orderllist = tasklist.get(position).getDetailList();
                viewHolder.num.setText("共" + orderllist.size() + "件商品");
                if(hasReason(orderllist)){
                    viewHolder.reason.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.reason.setVisibility(View.GONE);
                }
        }

        detaillist = tasklist.get(position).getOrderImageList();

        if (viewHolder.image_ll != null) {
            if (detaillist == null || detaillist.size() == 0) {
                viewHolder.image_ll.setVisibility(View.GONE);
            } else {
                viewHolder.image_ll.setVisibility(View.VISIBLE);
                showGrid(viewHolder.image_ll, tasklist.get(position).getOrderImageList());
            }
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    MallOrder order = tasklist.get(position);

                    Intent intent = new Intent(context, MallOrderDetailActivity.class);
                    intent.putExtra("order", order);
                    context.startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private boolean hasReason(List<OrderDetail> orderImageList) {
        for(OrderDetail order : orderImageList){
            if(!StringUtil.isEmpty(order.getReasons())){
                return true;
            }
        }
        return false;
    }

    private String statusToStr(String status){
        if(StringUtil.isEmpty(status) ){
            return "";
        } else {
            if(status.equals("SUBM")){
                return "新建";
            } else if(status.equals("KEEP")){
                return "审核";
            }else if(status.equals("0000")){
                return "已支付";
            }
            else if(status.equals("SNOE")){
                return "异常";
            }
        }
        return "";
    }

    private void showGrid(RecyclerView gridView, List<String> detaillist) {
        try {
            if (detaillist != null && detaillist.size() > 0) {
                LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(context);
                mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                gridView.setLayoutManager(mLinearLayoutManager);
                GridViewAdapter adapter = new GridViewAdapter(context,
                        detaillist);
                gridView.setAdapter(adapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getWidth(Activity context) {
        int length = 100;
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        return (int) (length * density);
    }

    public static int gridviewWidth(Activity context, int size) {
        int length = 100;
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        float density = dm.density;
        return (int) (size * (length + 4) * density);
    }


    public class DownloadTask extends AsyncTask<Void, Integer, Boolean> {

        RecyclerView mViewHolder;
        List<String> mDetaillist;

        public DownloadTask(RecyclerView viewHolder, List<String> detaillist) {
            mViewHolder = viewHolder;
            mDetaillist = detaillist;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                showGrid(mViewHolder, mDetaillist);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (result) {

            }
        }
    }


    /**
     * GirdView 数据适配器
     */
    public class GridViewAdapter extends RecyclerView.Adapter {
        Context context;
        List<String> list;

        public GridViewAdapter(Context _context, List<String> _list) {
            this.list = _list;
            this.context = _context;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.order_add_view_pic_item, null);
            Hodler vh = new Hodler(view);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            view.setLayoutParams(lp);
            return vh;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            final Hodler viewHolder = (Hodler) holder;
            String path = list.get(position);
            if (viewHolder.ItemImage != null) {
                if (!StringUtil.isEmpty(path)) {
                    Utils.glideImage(path, viewHolder.ItemImage);
                } else {
                    viewHolder.ItemImage.setVisibility(View.GONE);
                }
            }
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    class Hodler extends RecyclerView.ViewHolder {

        ImageView ItemImage;

        public Hodler(View convertView) {
            super(convertView);
            ItemImage = (ImageView) convertView.findViewById(R.id.ItemImage);
        }
    }


    class ViewHodler extends RecyclerView.ViewHolder {

        TextView tvDanju;
        TextView tvKehu;
        TextView tvShijian;
        TextView tvMoney;
        TextView danhao;
        TextView num;
        TextView reason;
        RecyclerView image_ll;
        RelativeLayout image_rl;

        public ViewHodler(View convertView) {
            super(convertView);
            tvDanju = (TextView) convertView.findViewById(R.id.danju);
            tvKehu = (TextView) convertView.findViewById(R.id.kehu);
            tvShijian = (TextView) convertView.findViewById(R.id.shijian);
            tvMoney = (TextView) convertView.findViewById(R.id.state);
            danhao = (TextView) convertView.findViewById(R.id.danhao);
            num = (TextView) convertView.findViewById(R.id.num);
            reason = (TextView) convertView.findViewById(R.id.reason);
            image_ll = (RecyclerView) convertView.findViewById(R.id.image_ll);
            image_rl = (RelativeLayout) convertView.findViewById(R.id.image_rl);
        }
    }
}